"""
请求协议：
    get请求：url，请求方法，请求头
    post请求:url，请求方法，请求头，请求体
"""
import requests

url = 'https://www.baidu.com/'
# 利用requests库，爬取网页数据，服务器返回的响应消息保存在r
r = requests.get(url)
print(r.text)
# r.text是str类型
print(type(r.text))
# r.content是bytes类型,可设置解码，decode()默认是utf-8
print(r.content.decode())
# 打印请求头
print(r.request.headers)
# 模拟浏览器，使用User-Agent
h = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
r2 = requests.get(url, headers=h)
print(r2.content.decode())

# 爬取搜索浙江工贸的结果
url_zjitc='https://www.baidu.com/s?wd=浙江工贸'
r3 = requests.get(url_zjitc, headers=h)
print(r3.content.decode())
# 将爬取数据保存在文件中
with open('zjitc.html','w',encoding='utf-8') as f:
    f.write(r3.content.decode())


